package com.zh.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.zh.utils.UserRealm;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Description: shiro 配置类
 * @ClassName ShiroConfig
 * @date: 2021.02.24 17:00
 * @Author: zhanghang
 */
@Configuration
public class ShiroConfig {
    // 下面两个方法对 注解权限起作用有很大的关系，请把这两个方法，放在配置的最上面
    @Bean(name = "lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }
    @Bean
    public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        autoProxyCreator.setProxyTargetClass(true);
        return autoProxyCreator;
    }


    /*第三步创建shiroFilterFactoryBean*/
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("defaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(defaultWebSecurityManager);
        //添加Shiro内置过滤器
        /*anon：无需认证就可以访问
         * authc：必须认证才能访问
         * 必须拥有记住我功能才能用
         * perms 拥有对某个资源的权限才能访问
         * role：拥有某个角色权限才能访问*/
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap();
        //添加权限,没有授权会跳转到为授权界面
//        filterChainDefinitionMap.put("/user/add","perms[user:add]");
//        filterChainDefinitionMap.put("/user/update","perms[user:update]");
//        filterChainDefinitionMap.put("/user/add","role[admin]");
//        filterChainDefinitionMap.put("/user/update","role[user]");
        filterChainDefinitionMap.put("/**","anon");
        bean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        /*设置登录的请求*/
        bean.setLoginUrl("/toLogin");
        //跳转未授权界面
        bean.setUnauthorizedUrl("/noauth");
        return bean;
    }
    /*第二步 创建securityManager*/
    @Bean(name = "defaultWebSecurityManager")
    public DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        //关联userRealm
        defaultWebSecurityManager.setRealm(userRealm);
        return defaultWebSecurityManager;
    }
    /*第一步创建realm对象 需要自定义类*/
    @Bean
    public UserRealm userRealm(){
        UserRealm userRealm = new UserRealm();
        // 设置密码加密规则
        userRealm.setCredentialsMatcher(hashedCredentialsMatcher());
        userRealm.setCachingEnabled(false);
        return userRealm;
    }

    /*
     * @describe 自定义凭证匹配器 配置密码比较器
     * （由于我们的密码校验交给Shiro的SimpleAuthenticationInfo进行处理了所以我们需要修改下doGetAuthenticationInfo中的代码）
     * 可以扩展凭证匹配器，实现输入密码错误次数后锁定等功能
     * @return org.apache.shiro.authc.credential.HashedCredentialsMatcher
     */
    @Bean(name = "credentialsMatcher")
    public HashedCredentialsMatcher  hashedCredentialsMatcher() {
        HashedCredentialsMatcher  hashedCredentialsMatcher = new HashedCredentialsMatcher ();
        hashedCredentialsMatcher.setHashAlgorithmName("MD5");//散列算法:这里使用MD5算法;
        hashedCredentialsMatcher.setHashIterations(1024);//散列的次数，比如散列两次，相当于 md5(md5(""));
        //storedCredentialsHexEncoded默认是true，此时用的是密码加密用的是Hex编码；false时用Base64编码
        // hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
        return hashedCredentialsMatcher;
    }

    //ShiroDialect用来整合shiro与thymeleaf
    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }
}
